# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json


# class FictionPipeline(object):
#     def process_item(self, item, spider):
#         if spider.name == "fictions":
#             if os.path.exists("./fiction1.txt"):
#                 os.mkdir("fiction1.txt")
#             for it in item:
#                 with open("fictions", 'a')as f:
#                     f.write(it)
#         return item
class FictionPipeline(object):
    def open_spider(self, spider):
        if spider.name == 'fictions':
            self.f = open('抬棺匠.txt', 'a', encoding='utf-8')

    def close_spider(self, spider):
        if spider.name == 'fictions':
            self.f.close()

    def process_item(self, item, spider):
        if spider.name == 'fictions':
            self.f.write(json.dumps(dict(item), ensure_ascii=False, indent=2) + '\n')
        return item
